<?php
/**
 * @desc 订单管理
 * @author kevin wang
 * @date 2014-09-20
 */
class ctrl_visit extends ctrl_base {
    private static $dirName = 'visit';

	private static $_visit_log_attr = array('序号','走访单位','走访地址','接待人姓名','接待人职务','接待人电话','接待人邮箱','走访日期','用户概况','反馈问题','需求信息','处理状态','处理日期','业务员ID号','业务员名称','负责区域','所在部门','负责产品','创建人','修改人','添加时间');
    
    /**
     * @desc 新闻列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('visit_before_list'));  	
		//订单获取
    	//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$sales_name = isset($_POST['sales_name'])? trim($_POST['sales_name']):'';
    	$visit_unit = isset($_POST['visit_unit'])? trim($_POST['visit_unit']):'';
    	//搜索条件组合
		$condition = array();
		if(!empty($sales_name)) {
    		$condition['sales_name'] = $sales_name;
    	}
		if(!empty($visit_unit)) {
    		$condition['visit_unit'] = $visit_unit;
    	}
		//订单获取
    	$visit_list = model_visit :: visitList($nextpage,$pagesize,$condition);
    	$total = array_pop($visit_list);
		//模板赋值
    	self::smarty() -> assign('visit_list', $visit_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('sales_name', $sales_name);
		self::smarty() -> assign('visit_unit', $visit_unit);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/visit_list.html');    	
    }
	//添加走访信息
	public static function visitAdd(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('visit_before_add'));
		if($_POST){

			$visit = self::get_request_data();
			$visit['editer'] = $_SESSION['admin']['username'];
			$visit['author'] = $_SESSION['admin']['username'];
			self::limit_weixin_id($visit['sales_id']);

			$result = model_visit::visitAdd($visit);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','visit_log','closeCurrent');
			}else{
				self::ajax_ouput(300, '添加失败');
			}
		}
		self::smarty() -> display(self::$dirName . '/visit_add.html');
	}
	//删除订单
	public static function visitDelete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('visit_before_delete'));
    	$ids = $_POST['ids'];
		$ids = explode(",", $ids);
		foreach($ids as $v){
			$id[] = intval($v);
		}
    	$data = array('delete_flag' => 1);

    	$st = model_visit::updateVisitInfo($id, $data);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','visit_log');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }
	//编辑订单
	 public static function visitEdit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$visit = model_visit::getVisitInfoById($id);
		$ctrl_tag = 'visit_before_edit_other';
		if($visit['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'visit_before_edit_self';
		}
		self::operation(array($ctrl_tag));
    	if($_POST){
			$visit = array();

			$visit = self::get_request_data();
			$visit['editer'] = $_SESSION['admin']['username'];
			self::limit_weixin_id($visit['sales_id']);

			unset($visit['id']);

			$result = model_visit::updateVisitInfo(explode(',',$id), $visit);
	    	
		    if(!empty($result)){
				self::ajax_ouput(200, '操作成功','visit_log','closeCurrent');
			}else{
				self::ajax_ouput(300, '添加失败');
			}    		
    	}
    	self::smarty() -> assign('id', $id);
    	self::smarty() -> assign('visit', $visit);
    	self::smarty() -> display(self::$dirName . '/visit_edit.html');
    }
	//导出访问记录
	public static function visitExport(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('visit_before_export'));
    	//页面输入参数处理
    	$currentPage = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
    	$pagesize = isset($_GET['numPerPage']) ? $_GET['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$sales_name = isset($_GET['sales_name'])? trim($_GET['sales_name']):'';
    	$visit_unit = isset($_GET['visit_unit'])? trim($_GET['visit_unit']):'';
    	//搜索条件组合
		$condition = array();
		if(!empty($sales_name)) {
    		$condition['sales_name'] = $sales_name;
    	}
		if(!empty($visit_unit)) {
    		$condition['visit_unit'] = $visit_unit;
    	}
		//订单获取
    	$visit_list = model_visit :: visitList($nextpage,$pagesize,$condition);
    	$total = array_pop($visit_list);
		array_unshift($visit_list,self::$_visit_log_attr);
		//var_dump($visit_list);exit;
		//载入xls处理类
		$time = time();
		$title = "售前走访信息";
		$subject = "visit log-{$_SESSION['admin']['username']}-{$time}";
		$descript = "visit log-{$_SESSION['admin']['username']}-{$time}";
		$username = $_SESSION['admin']['username'];
		framework_base_autoloader::import_vendor('excel/PHPExcel.php');
		$objPHPExcel = new PHPExcel();
		//设置属性
		$objPHPExcel->getProperties()->setCreator($username);
		$objPHPExcel->getProperties()->setLastModifiedBy($username);
		$objPHPExcel->getProperties()->setTitle($title);
		$objPHPExcel->getProperties()->setSubject($subject);
		$objPHPExcel->getProperties()->setDescription($descript);
		//添加数据
		$objPHPExcel->setActiveSheetIndex(0);
		$cellFirst = 65;
		foreach($visit_list as $keys => $values){
			$i = 0;
			foreach($values as $k=>$v){
				if($k == 'visit_log_id')continue;
				$parentKey = chr($cellFirst + $i);
				$cellKey = $parentKey.($keys+1);
				$cellVlue = $visit_list[$keys][$k];
				$objPHPExcel->getActiveSheet()->SetCellValue($cellKey, $cellVlue);
				$i++;
			}
		}
		// Rename sheet
		$objPHPExcel->getActiveSheet()->setTitle($title);
		switch(self::$_excel_pre){
			case '.xls':$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);break;
			case '.xlsx':$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);break;//Save Excel 2007 file
			default:$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);break;
		}		 
		$down = self::get_down_file_url(self::$_excel_pre);
		$objWriter->save($down['save']);
		self::download_xls_output($down['save'], $down['down']); 
	}
}
